import 'package:distribution_mall_flutter/base/base_service.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/http/modules/user.dart';
import 'package:distribution_mall_flutter/provider/user/login.dart';
import 'package:distribution_mall_flutter/router/router.gr.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'login_service.g.dart';

class LoginService extends BaseService {
  LoginService(super.ref);

  Future<void> checkInviteCode() async {
    (await UserApi.distributorInfo()).yes((value) async {
      ///没有邀请码
      if (value?.parentInvitationCode == null) {
        bool? result = await appRouter.push(const LoginInviteCodeRoute());
        println('resultparentInvitationCode ${result}');
        if(result != true){
          ref
              .watch(loginStateProvider.notifier)
              .outLogin(go2login: false);
        }
        if(appRouter.current.name == LoginVerificationRoute.name){
          await appRouter.maybePop();
        }
        await appRouter.maybePop();
      } else {
        if(appRouter.current.name == LoginVerificationRoute.name){
          await appRouter.maybePop();
        }
        await appRouter.maybePop();
      }
    });
  }
}

@riverpod
LoginService loginService(LoginServiceRef ref) {
  return LoginService(ref);
}
